package com.xujl.utilslibrary.data;


import com.xujl.utilslibrary.validator.VaBoolean;
import com.xujl.utilslibrary.validator.VaGroup;
import com.xujl.utilslibrary.validator.VaNotEmpty;
import com.xujl.utilslibrary.validator.VaOrder;
import com.xujl.utilslibrary.validator.VaTest;
import com.xujl.utilslibrary.validator.VaUtils;
import com.xujl.utilslibrary.view.ViewTool;

import java.lang.reflect.Field;
import java.util.Arrays;

/**
 * Created by xujl on 2018/1/3.
 * 验证工具测试类
 */

public class ValidationTest {

    //        测试代码
    public static void main (String[] arg) {
        final Bean bean = new Bean("1", "", "1", "1", "1");
        bean.bean = new Bean2("1", "1",false);
        try {
            final VaUtils vaUtils = new VaUtils();
            //自定义验证
            vaUtils.setVaCallbacks(Arrays.<VaUtils.VaCallback>asList(new VaUtils.VaCallback() {
                @Override
                public void validator (Field field, String fieldType, Object obj) throws DataException, IllegalAccessException {
                    final VaTest annotation = field.getAnnotation(VaTest.class);
                    //没有此注解时退出
                    if (annotation == null) {
                        return;
                    }
                    String value = (String) field.get(obj);
                    if (ViewTool.isEmpty(value)) {
                        throw new DataException(annotation.msg());
                    }
                }
            }));
            vaUtils.validatorAll(bean,1,2);
            System.err.println("验证通过");
        } catch (DataException e) {
            System.err.println(e.getMessage());
        }
    }

    public static class Bean {
        @VaOrder(1)
        @VaNotEmpty(msg = "name用户名不能为空！")
        @VaGroup({1})
        private String name;
        private Bean2 bean;
        @VaOrder(2)
        @VaGroup({1,2})
        @VaNotEmpty(msg = "pass用户名不能为空！")
        private String pass;
        @VaOrder(3)
        @VaGroup({1,2})
        @VaNotEmpty(msg = "pass2用户名不能为空！")
        private String pass2;
        @VaOrder(4)
        @VaNotEmpty(msg = "pass3用户名不能为空！")
        private String pass3;
        @VaOrder(5)
        @VaNotEmpty(msg = "pass4用户名不能为空！")
        private String pass4;


        public Bean (String name, String pass, String pass2, String pass3, String pass4) {
            this.name = name;
            this.pass = pass;
            this.pass2 = pass2;
            this.pass3 = pass3;
            this.pass4 = pass4;
        }
    }

    public static class Bean2 {
        @VaOrder(1)
        @VaTest(msg = "Bean2name用户名不能为空！")
        private String name2;
        @VaOrder(2)
        @VaNotEmpty(msg = "Bean2pass用户名不能为空！")
        private String pass2;
        @VaOrder(3)
        @VaBoolean(msg = "不能为false")
        private boolean isCheck;

        public Bean2 (String name, String pass, boolean isCheck) {
            this.name2 = name;
            this.pass2 = pass;
            this.isCheck = isCheck;
        }
    }
}
